package com.bayihouse.utils;

import com.bayihouse.common.ErrorCode;
import com.bayihouse.exception.BusinessException;
import com.bayihouse.model.entity.*;
import com.bayihouse.model.enums.RoomRoleEnum;
import com.bayihouse.model.excel.*;
import com.bayihouse.model.excel.publicClass.ExcelResult;
import com.bayihouse.service.*;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Component
@Data
public class ExcelCommunityUtils {


    @Resource
    private AdministerService administerService;


    @Resource
    private OccupantService occupantService;


    @Resource
    private RoomService roomService;

    @Resource
    private UnitService unitService;

    @Resource
    private HouseService houseService;

    @Resource
    private CommunityService communityService;

    @Resource
    private NetObjectService netObjectService;


    private void setRoomFields(ExcelRoom excelRoom) {

    }


    public Netobject getNetobject(Set<Netobject> set, String communityName) {
        Netobject netobject = new Netobject();
        netobject.setName(communityName);
        if (set.contains(netobject)) {
            for (Netobject netobject1 : set) {
                if (netobject1.getName().equals(communityName)) {
                    return netobject1;
                }
            }
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "ExcelCommunityUtils,中getNetObject方法出现问题了");
        } else {
            netObjectService.save(netobject);
            set.add(netobject);
            return netobject;
        }
    }

    public Community findCommunity(List<Community> communityList, String communityName, String communityAddress, RoomRoleEnum houseRole, Netobject netobject) {
        ExcelResult<Community> excelResult = MyObjectUtils.findObject(communityList, communityName);
        Community result = excelResult.getResult();
        if (result == null) {
            result = new Community();
            result.setName(communityName);
            result.setAddress(communityAddress);
            result.setNetId(netobject.getId());
            if (houseRole == RoomRoleEnum.LIVING_USING) {
                result.InitT();
            } else {
                result.InitV();
            }
            MyObjectUtils.addObject(communityList, result, excelResult.getIndex());
        }
        return result;
    }


    public House findHouse(Community targetCommunity, String houseName) {
        List<House> houseList = targetCommunity.getTlist();
        ExcelResult<House> houseResult = MyObjectUtils.findObject(houseList, houseName);
        House result = houseResult.getResult();
        if (result == null) {
            result = new House();
            result.setName(houseName);
            result.setCommunityId(targetCommunity.getId());
            result.initT();
            MyObjectUtils.addObject(houseList, result, houseResult.getIndex());
        }
        return result;
    }


    public Unit findUnit(House targetHouse, String unitName) {
        List<Unit> unitList = targetHouse.getTlist();
        ExcelResult<Unit> unitResult = MyObjectUtils.findObject(unitList, unitName);
        Unit result = unitResult.getResult();
        if (result == null) {
            result = new Unit();
            result.setName(unitName);
            result.setHouseId(targetHouse.getId());
            result.initT();
            MyObjectUtils.addObject(unitList, result, unitResult.getIndex());
        }
        return result;
    }


    public Room findRoom(Unit targetUnit, String roomName, Netobject netobject, Community targetCommunity, House targetHouse) throws Exception {
        List<Room> roomList = targetUnit.getTlist();
        ExcelResult<Room> roomExcelResult = MyObjectUtils.findObject(roomList, roomName);
        Room room = roomExcelResult.getResult();
        if (room == null) {
            room = new Room();
            room.setName(roomName);
            room.setName(roomName);
            room.initT();
            MyObjectUtils.addObject(roomList, room, roomExcelResult.getIndex());
        }
        return room;
    }

    public Room findRoom(Unit unit, String roomName, Netobject netobject, Community community, House house, Occupant occupant) throws Exception {
        Room targetRoom = findRoom(unit, roomName, netobject, community, house);
        targetRoom.getChildrenList().add(occupant);
        return targetRoom;
    }


    public Room findBusinessRoom(Community community, String roomName, Occupant occupant) {
        Room targetRoom = findBusinessRoom(community, roomName);
        targetRoom.getChildrenList().add(occupant);
        return targetRoom;
    }


    public Room findBusinessRoom(Community targetCommunity, String roomName) {
        List<Room> roomList = targetCommunity.getVlist();
        ExcelResult<Room> roomExcelResult = MyObjectUtils.findObject(roomList, roomName);
        Room room = roomExcelResult.getResult();
        if (room == null) {
            room = new Room();
            room.setName(roomName);
            room.initT();
            MyObjectUtils.addObject(roomList, room, roomExcelResult.getIndex());
        }
        return room;
    }


    public void addExcelOccupation(List<Occupant> occupantList, Occupant occupant, List<Community> communityList, Netobject netobject) throws Exception {
        List<String> collect = communityList.stream().map(Community::getName).collect(Collectors.toList());
        System.out.println(Arrays.toString(collect.toArray()));
        String nowAddress = occupant.getNowAddress();
        boolean businessUsing = AddressUtils.isBusinessUsing(nowAddress);
        Room waitAddRoom = null;
        if (businessUsing) {
            String[] nowAddressArray = AddressUtils.getNowAddressArrayBusiness(nowAddress);
            String communityName = "商业区";
            String communityAddress = communityName;
            String roomName = nowAddressArray[2];
            Community resCommunity = findCommunity(communityList, communityName, communityAddress, RoomRoleEnum.BUSINESS_USING, netobject);
            waitAddRoom = findBusinessRoom(resCommunity, roomName, occupant);
        } else {
            String[] nowAddressArrayLiving = AddressUtils.getNowAddressArrayLiving(nowAddress);
            String communityName = nowAddressArrayLiving[1];
            String communityAddress = nowAddressArrayLiving[0];
            String houseName = nowAddressArrayLiving[2];
            String unitName = nowAddressArrayLiving[3];
            String roomName = nowAddressArrayLiving[4];
            Community excelCommunity = findCommunity(communityList, communityName, communityAddress, RoomRoleEnum.LIVING_USING, netobject);
            House excelHouse = findHouse(excelCommunity, houseName);
            Unit excelUnit = findUnit(excelHouse, unitName);
            waitAddRoom = findRoom(excelUnit, roomName, netobject, excelCommunity, excelHouse, occupant);
        }
        if (StringUtils.isBlank(waitAddRoom.getAddress())) {
            waitAddRoom.setAddress(nowAddress);
        }
        occupantList.add(occupant);
    }
}
